package LCR_code;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author blh
 * @date 2024年08月05日 9:55
 */
public class LCR_167_85 {
    public int dismantlingAction(String arr) {
        int len = arr.length();
        // 显然是双指针
        if (len == 0) {
            return 0;
        }
        char[] chars = arr.toCharArray();
        int maxLen = 1;

        HashMap<Character, Integer> seen = new HashMap<>();
        seen.put(chars[0], 0);

        for (int i = 1; i < arr.toCharArray().length; i++) {
            if (!seen.containsKey(chars[i])) {
                seen.put(chars[i], i);
            } else {
                int index = seen.get(chars[i]);
                // 将seen中所有value小于index的键值对都删掉
                List<Character> remove = new ArrayList<>();
                for (Map.Entry<Character, Integer> entry : seen.entrySet()) {
                    if (entry.getValue() <= index) {
                        remove.add(entry.getKey());
                    }
                }
                for (Character c : remove) {
                    seen.remove(c);
                }
            }
            maxLen = Math.max(maxLen, seen.size());
        }
        return maxLen;
    }
}
